package com.jiancong.utils;

import com.jiancong.domain.entity.LoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @Author 三更  B站： https://space.bilibili.com/663528522
 */
public class SecurityUtils
{

    /**
     * 获取用户
     **/
    public static LoginUser getLoginUser()
    {
        Object principalObject = getAuthentication().getPrincipal();
        if (principalObject instanceof LoginUser)
        {
            return (LoginUser) principalObject;
        }
        else
        {
            // 这里可以根据业务需求进行处理，比如抛出异常或者返回一个默认的LoginUser对象（如果有）
            throw new RuntimeException("获取的认证主体对象不是LoginUser类型");
        }
    }

    /**
     * 获取Authentication
     */
    public static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    public static Boolean isAdmin(){
        Long id = getLoginUser().getUser().getId();
        return id != null && id.equals(1L);
    }

    public static Long getUserId() {
        return getLoginUser().getUser().getId();
    }
}